package com.zyl.courseservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyl.commonbase.constant.CourseConstant;
import com.zyl.commonutils.ResponseResult;
import com.zyl.courseservice.entity.TCourse;
import com.zyl.courseservice.service.TCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**

 * @author 略
 * @since 2020-12-08
 */
@Api( description = "课程管理")
@CrossOrigin
@RestController
@RequestMapping("/course")
public class TCourseController {

    private TCourseService courseService;
    public TCourseController(TCourseService courseService){ this.courseService=courseService;}

    @ApiOperation("所有课程列表")
    @GetMapping({"findAll"})
    public ResponseResult findAllCourse() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("course_status", CourseConstant.COURSE_ENABLE);
        List list = this.courseService.list(wrapper);
        return ResponseResult.ok().data("items", list);
    }

    @ApiOperation("分页查询审核通过的课程列表")
    @GetMapping({"pageCourse/{current}/{limit}"})
    public ResponseResult pageCourse(@PathVariable long current, @PathVariable long limit,
                                      @RequestBody( required = false )TCourse course) {
        Page pageCourse = new Page(current, limit);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("course_status", CourseConstant.COURSE_ENABLE);
        this.courseService.page(pageCourse, wrapper);
        long total = pageCourse.getTotal();
        List records = pageCourse.getRecords();
        return ResponseResult.ok().data("total", Long.valueOf(total)).data("rows", records);
    }

    @ApiOperation("分页查询所有课程列表")
    @GetMapping({"pageCourseAll/{current}/{limit}"})
    public ResponseResult pageCourseAll(@PathVariable long current, @PathVariable long limit,
                                      @RequestBody( required = false )TCourse course) {
        Page pageCourse = new Page(current, limit);
        courseService.page(pageCourse,null);
        long total =pageCourse.getTotal();
        List records = pageCourse.getRecords();
        return ResponseResult.ok().data("total", Long.valueOf(total)).data("rows", records);
    }
    @ApiOperation("新增课程")
    @PostMapping({"/addCourse/"})
    public ResponseResult addCourse(@RequestBody TCourse course) {

        boolean save = this.courseService.save(course);
        return save? ResponseResult.ok(): ResponseResult.error();
    }

    @ApiOperation("根据ID删除课程")
    @DeleteMapping({"{id}"})
    public ResponseResult removeById(@ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable int id) {
        boolean flag = this.courseService.removeById(id);
        return flag? ResponseResult.ok(): ResponseResult.error();
    }

    @ApiOperation("根据ID修改课程")
    @PostMapping({"updateCourse"})
    public ResponseResult updateCourse(
            @ApiParam(name = "class",value = "班级对象",required = true)@RequestBody TCourse course,
            @ApiParam(name = "id", value = "课程ID", required = true)@PathVariable int id) {
        course.setCourseId(id);
        boolean flag = this.courseService.updateById(course);
        return flag? ResponseResult.ok(): ResponseResult.error();
    }
}

